/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import common.Settings;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;
import service.arbitrator.ArbitratorDetails;

/**
 *
 * @author Danny
 */
class ArbitratorsTableModel extends AbstractTableModel {

    ArbitratorDetails[] ad;
   public Set<Integer> userAcceptableArbitrators;
  public   Set<Integer> serverAcceptableArbitrators;
    private JButton button;

    public ArbitratorsTableModel() {
        this.ad = new ArbitratorDetails[0];
        this.userAcceptableArbitrators = new HashSet<Integer>();
        this.serverAcceptableArbitrators = new HashSet<Integer>();
    }

    ArbitratorsTableModel(ArbitratorDetails[] ad1, JButton button1) {
        this.button = button1;
        this.ad = ad1;
        this.userAcceptableArbitrators = new HashSet<Integer>();
        this.serverAcceptableArbitrators = new HashSet<Integer>();
    }

    public int getRowCount() {
        return ad.length;
    }

    public int getColumnCount() {
        return 5;
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Arbitrator ID";
            case 1:
                return "Users rating";
            case 2:
                return "Servers rating";
            case 3:
                return "Acceptable ?";
            case 4:
                return "Server acceptable?";
        }
        return "ArbitratorsTableModel- getColumnName";

    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 3:
                return Boolean.class;
            case 4:
                return Boolean.class;
        }
        return Integer.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex == 3) {
            return true;
        }
        return false;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (columnIndex == 3) {
            if ((Boolean) aValue) {
                this.userAcceptableArbitrators.add(Integer.parseInt(((String)getValueAt(rowIndex, 0)).substring(11)));
            } else {
                this.userAcceptableArbitrators.remove(Integer.parseInt(((String)getValueAt(rowIndex, 0)).substring(11)));//(getValueAt(rowIndex, 0));
            }
        }
        if (this.userAcceptableArbitrators.size() >= Settings.sizeOfAgreementSet) {
            this.button.setEnabled(true);
        } else {
            this.button.setEnabled(false);
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Arbitrator "+ad[rowIndex].getArbitratorID();
            case 1:
                return ad[rowIndex].getUserScore();
            case 2:
                return ad[rowIndex].getServerScore();
            case 3:
                if (this.userAcceptableArbitrators.contains(Integer.parseInt(((String)getValueAt(rowIndex, 0)).substring(11)))){//(getValueAt(rowIndex, 0))) {
                    return true;
                } else {
                    return false;
                }
                case 4:
                if (this.serverAcceptableArbitrators.contains(Integer.parseInt(((String)getValueAt(rowIndex, 0)).substring(11)))){//(getValueAt(rowIndex, 0))) {
                    return true;
                } else {
                    return false;
                }
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
